//
//  LSZNewTabbarAudioVC+LCCycle.m
//  JKFuYinProject
//
//  Created by admin on 2018/4/12.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZNewTabbarAudioVC+LCCycle.h"

@implementation LSZNewTabbarAudioVC (LCCycle)


- (UIView *)setupCycleWithCycleHeight:(CGFloat)cycleHeight {
    [self addPagerViewWithCycleHeight:cycleHeight];
    [self addPageControl];
    return self.pagerView;
}

- (UIView *)addPagerViewWithCycleHeight:(CGFloat)cycleHeight {
  

    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc] init];
    self.pagerView = pagerView;
    pagerView.isInfiniteLoop = YES;
    pagerView.autoScrollInterval = 5.0;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    
    [pagerView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:@"cellId"];

    self.pagerView.frame = CGRectMake(10, 10, CGRectGetWidth(self.view.frame)-20, cycleHeight);
    [self.pagerView.layer setMasksToBounds:YES];
    [self.pagerView.layer setCornerRadius:4];
    self.pagerView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.95 alpha:1.00];
    return self.pagerView;
}
- (void)addPageControl {
    TYPageControl *pageControl = [[TYPageControl alloc]init];
    self.pageControl = pageControl;
    self.pageControl.frame = CGRectMake(0, CGRectGetHeight(self.pagerView.frame) - 26, CGRectGetWidth(self.pagerView.frame), 26);
    
    pageControl.currentPageIndicatorSize = CGSizeMake(8, 8);
    //    pageControl.pageIndicatorImage = [UIImage imageNamed:@"Dot"];
    //    pageControl.currentPageIndicatorImage = [UIImage imageNamed:@"DotSelected"];
    //    pageControl.contentInset = UIEdgeInsetsMake(0, 20, 0, 20);
    //    pageControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    //    pageControl.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //    [pageControl addTarget:self action:@selector(pageControlValueChangeAction:) forControlEvents:UIControlEventValueChanged];
    [self.pagerView addSubview:pageControl];
}

#pragma mark - TYCyclePagerViewDelegate

///pagerView did selected item cell
- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(UICollectionViewCell *)cell atIndex:(NSInteger)index{
    if (self.cycleArray == nil) {
        return;
    }
    [self didSelectItemAtIndex:index];
    NSLog(@"index = %ld", (long)index);
}

#pragma mark - 轮播图的点击事件
- (void)didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"index = %ld", (long)index);
    LSZNewAudioPlayImageAndTableViewVC * vc = [[LSZNewAudioPlayImageAndTableViewVC alloc]init];
    vc.strTopImageView = self.cycleArray[index].image;
    vc.strLastImageView = self.cycleArray[index].image;
    vc.titleStr = self.cycleArray[index].singerName;
    vc.type = @"zhuanji";
    vc.typeStr = @"zhuanji";
    vc.parentId = self.cycleArray[index].id;
    vc.typeComeWhere = @"1";
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - TYCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    if (self.cycleArray == nil) {
        self.pagerView.hidden = YES;
        return 0;
    }
    self.pagerView.hidden = NO;
    return self.cycleArray.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    TYCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    LCCycleModel *cycleDict = self.cycleArray[index];
    NSURL *url = [NSURL URLWithString:cycleDict.image];
    if (self.cycleArray == nil) {
        cell.imageViewBack.image = [UIImage imageNamed:@"devotion_bannerA"];
    }else{
        [cell.imageViewBack yy_setImageWithURL:url placeholder:[UIImage imageNamed:@"devotion_bannerA"]];
    }
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)/***0.92*/, CGRectGetHeight(pageView.frame)/***0.945*/);
    layout.itemSpacing = 0;//5.5;
    layout.itemHorizontalCenter = YES;
    layout.layoutType = 1;
    layout.minimumScale = 1;//0.905;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    self.pageControl.currentPage = toIndex;
    //[_pageControl setCurrentPage:newIndex animate:YES];
    //    NSLog(@"%zd ->  %zd",fromIndex,toIndex);
}

-(TYPageControl *)pageControl{
    return objc_getAssociatedObject(self, &pageControlKey);
}
-(void)setPageControl:(TYPageControl *)pageControl{
    objc_setAssociatedObject(self, &pageControlKey, pageControl, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(void)loadDataWithPage:(BOOL)page{
    
    NetworkTool *networkTool = [NetworkTool sharedInstance];
    NSString *urlString = [NSString stringWithFormat:@"%@/music/slide",http_zongSJUrl];
    NSDictionary *parameters = @{};
    [networkTool POSTFuYinWithUrlString:urlString input:parameters success:^(NSDictionary *dict) {
        NSArray *dictArr2 = dict[@"data"];
        self.cycleArray = [NSArray yy_modelArrayWithClass:[LCCycleModel class] json:dictArr2];
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self loadCycleScrollViewData];
        }];
    } failure:^(NSError *error) {
        NSLog(@"error = %@", error);
    }];
    
}

-(void)loadCycleScrollViewData{
    self.pageControl.numberOfPages = self.cycleArray.count;
    [self.pagerView reloadData];
}

@end
